package cn.schoolwow.util.test.module.compare.jsonarray.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.compare.jsonarray.CompareJSONArray;
import cn.schoolwow.util.domain.compare.jsonarray.CompareJSONArrayResponse;
import cn.schoolwow.util.domain.compare.jsonarray.UpdateJSONArrayCompare;
import com.alibaba.fastjson.JSONArray;
import org.junit.Assert;

import java.util.List;

public class CompareJSONArrayQueryProcessFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCompareJSONArrayResponse(flowContext);
        checkAddCompareList(flowContext);
        checkUpdateCompareList(flowContext);
        checkDeleteJSONArray(flowContext);
    }

    @Override
    public String name() {
        return "比较JSON列表";
    }

    private void setCompareJSONArrayResponse(FlowContext flowContext){
        JSONArray beforeJSONArray = (JSONArray) flowContext.checkData("beforeJSONArray");
        JSONArray afterJSONArray = (JSONArray) flowContext.checkData("afterJSONArray");

        CompareJSONArrayResponse compareJSONArrayResponse = CompareJSONArray.newInstance()
                .beforeJSONArray(beforeJSONArray)
                .afterJSONArray(afterJSONArray)
                .addCheckFieldNames(new String[]{"name"})
                .updateCheckFieldNames(new String[]{"name", "deadline"})
                .execute();
        flowContext.putCurrentFlowData("compareJSONArrayResponse", compareJSONArrayResponse);
    }

    private void checkAddCompareList(FlowContext flowContext){
        CompareJSONArrayResponse compareJSONArrayResponse = (CompareJSONArrayResponse) flowContext.checkData("compareJSONArrayResponse");
        JSONArray expectAddCompareArray = (JSONArray) flowContext.checkData("addCompareArray");

        JSONArray addCompareList = compareJSONArrayResponse.getAddCompareArray();
        Assert.assertEquals(expectAddCompareArray, addCompareList);
    }

    private void checkUpdateCompareList(FlowContext flowContext){
        CompareJSONArrayResponse compareJSONArrayResponse = (CompareJSONArrayResponse) flowContext.checkData("compareJSONArrayResponse");
        List<UpdateJSONArrayCompare> updateJSONArrayCompareArray = (List<UpdateJSONArrayCompare>) flowContext.checkData("updateJSONArrayCompareArray");

        List<UpdateJSONArrayCompare> updateJSONArrayCompareList = compareJSONArrayResponse.getUpdateJSONArrayCompareList();
        Assert.assertEquals(updateJSONArrayCompareArray.size(), updateJSONArrayCompareList.size());
        for(int i=0;i<updateJSONArrayCompareList.size();i++){
            UpdateJSONArrayCompare updateJSONArrayCompare = updateJSONArrayCompareList.get(i);
            UpdateJSONArrayCompare expectUpdateJSONArrayCompare = updateJSONArrayCompareArray.get(i);

            Assert.assertEquals(expectUpdateJSONArrayCompare.beforeObject, updateJSONArrayCompare.beforeObject);
            Assert.assertEquals(expectUpdateJSONArrayCompare.afterObject, updateJSONArrayCompare.afterObject);
        }
    }

    private void checkDeleteJSONArray(FlowContext flowContext){
        CompareJSONArrayResponse compareJSONArrayResponse = (CompareJSONArrayResponse) flowContext.checkData("compareJSONArrayResponse");
        JSONArray expectDeleteCompareList = (JSONArray) flowContext.checkData("deleteCompareArray");

        JSONArray deleteCompareList = compareJSONArrayResponse.getDeleteCompareArray();
        Assert.assertEquals(expectDeleteCompareList.size(), deleteCompareList.size());
        Assert.assertEquals(expectDeleteCompareList.toJSONString(), deleteCompareList.toJSONString());
    }
}
